package com.peanut.baby.mvp.main.expert;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.peanut.baby.R;
import com.peanut.baby.mvp.main.BaseHomeFragment;
import com.peanut.baby.mvp.main.expert.live.MainLiveListFragment;
import com.peanut.baby.mvp.main.expert.qa.MainQaFragment;
import com.peanut.devlibrary.adapter.FragmentTabAdapter;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/7/11.
 */

public class ExpertFragment extends BaseHomeFragment implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {
    private static final String TAG = ExpertFragment.class.getSimpleName();
    @BindView(R.id.expert_radio_live)
    RadioButton expertRadioLive;
    @BindView(R.id.expert_radio_qa)
    RadioButton expertRadioQa;
    @BindView(R.id.expert_radio_group)
    RadioGroup expertRadioGroup;
    @BindView(R.id.expert_viewpager)
    ViewPager expertViewpager;
    Unbinder unbinder;

    ArrayList<Fragment> fragments;
    FragmentTabAdapter adapter;

    private MainLiveListFragment liveFragment;
    private MainQaFragment qaFragment;

    private int defaultIndex = 0;

    public static ExpertFragment newInstance() {
        return new ExpertFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_expert, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        expertViewpager.setOnPageChangeListener(this);
        expertRadioGroup.setOnCheckedChangeListener(this);


    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        liveFragment = MainLiveListFragment.newInstance();
        qaFragment = MainQaFragment.newInstance();
        fragments.add(liveFragment);
        fragments.add(qaFragment);
        adapter = new FragmentTabAdapter(getChildFragmentManager(), fragments);
        expertViewpager.setAdapter(adapter);
        expertViewpager.setOffscreenPageLimit(2);
        setCurrent(defaultIndex);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void setCurrent(int currentIndex) {

        RadioButton radioButton = (RadioButton) expertRadioGroup.getChildAt(currentIndex);
        if (!radioButton.isChecked()) {
            radioButton.setChecked(true);
        }

        if (expertViewpager.getCurrentItem() != currentIndex) {
            expertViewpager.setCurrentItem(currentIndex);
        }

    }

    public void setDefaultIndex(int index){
        defaultIndex = index;
        if(isActive()){
            setCurrent(index);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.expert_radio_live:
                setCurrent(0);
                break;
            case R.id.expert_radio_qa:
                setCurrent(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void refreshFragmentData() {
        Log.d(TAG,"refreshFragmentData");
        if(liveFragment != null && liveFragment.isAdded()){
            liveFragment.initData();
        }
        if(qaFragment != null && qaFragment.isAdded()){
            qaFragment.initData();
        }
    }
}
